<?php
declare (strict_types = 1);

namespace app\middleware;
use think\Request;
use think\Validate;

class OutsideAccessAuthorityMiddleware
{
    public function handle(Request $request, \Closure $next)
    {
        $token = $request->header('token');
        if (empty($token)) $token = $request->param('token');// get|post

        $check_data = ['token' => $token];// 验证参数
        $rules      = ['token' => 'require|alphaNum|length:32, 128'];// 设置验证规则
        $validate   = new Validate;
        // 验证数据,验证通过返回 true
        if($validate->check($check_data, $rules) === false) return json(['code' => 0, 'message' => 'token 错误']);
        if (config('app.my_token') != $token) return json(['code' => 0, 'message' => 'token 错误']);

        return $next($request);
    }
}
